
//////////////////////// Page Initializing ////////////
//
jQuery(document).ready(function() {	
	$('.nav_bg a').click(function() {
		$('.nav_bg a').removeClass('active');
		$(this).addClass('active');
	});		
								
});


//////////////////////// LOADING A PAGE ////////////
//

function loadPage(sourceDiv, actionURL, serviceId) {		
	$.ajax ({		
		url:	  	actionURL + "?set_ajax_view",
		data:		"service_id=" + serviceId,		
		dataType: 	"html",
		beforeSend: function() {					
			
			$('div#pageContent').hide("slow");
			$('div#' + sourceDiv).hide("slow");			
		},
		complete: 	function() {
			$('div#' + sourceDiv).show("fast");			
		},
		success:  function(data) {
			$("#" + sourceDiv).html(data);			
		}
	});		
}

function editServiceGroup(service_group_id, name){
	$('div#add_form').fadeOut("fast");
	$('#status_message').text('').hide();
	$('#name').css("border-color", "#AAA");
	$('#service_group_id').val(service_group_id);
	$('#name').val(name);
	$('#button_add').hide();
	$('#button_save').show();
	$('#button_cancel').show();
	$('div#add_form').fadeIn("slow");
	window.scrollTo(0,0);
}

function cancelServiceGroup() {
	$('div#add_form').fadeOut("fast");
	$('#status_message').text('').hide();
	$('#name').css("border-color", "#AAA");
	$('#service_group_id').val('');
	$('#name').val('');
	$('#button_add').show();
	$('#button_save').hide();
	$('#button_cancel').hide();
	$('div#add_form').fadeIn("slow");
}

function saveServiceGroup() {
	// refresh status_message
    $('#status_message').html("");
    // prepare data before saving
    
    // check service_group_name
    if (!isNotEmpty($('#name'), _("Empty service group name"), "status_message")) {
        return;
    }
    $.ajax({
        type:   "GET",
        url:    "../ServiceGroup/CheckServiceGroupNameAjax?set_ajax_view",
        data:   'name=' + $('#name').val() +
        		'&service_group_id=' + $('#service_group_id').val() ,        
        success:    function(data) {
        	if (data == 0) {	
        		$('#status_message').text(_('Duplicated service group')).show();
				$('#name').css("border-color", "red");
        	}else{
        		submitSave();
        	}        	
        }
    });
}
// send request after checked fields 
function submitSave(){
    
	var submitData = $('#add_service_group_form').formSerialize();
    // call controller to save data
    $.ajax({
        type:   "POST",
        url:    "../ServiceGroup/SaveServiceGroupAjax?set_ajax_view",
        data:   submitData,
        beforeSend: function() {
            $('div#pageContent').hide();
            $('#barLoading').show();                
        },
        complete:   function() {
            $('#barLoading').hide();
            
        },
        success:    function(data) {
        	loadPage('pageContent', 'ServiceGroupListAjax', '');
        }
    });
}
// get mapped services and unmapped services of service group
function getServices(){
	$.ajax({
        type:   "GET",
        url:    "../ServiceGroup/GetServicesAjax?set_ajax_view",
        data:   "&groupId=" + $('#ServiceGroup').val(),
        beforeSend: function() {
            $('div#search_button').hide();
			$('div#search_button').next().show();        
        },
        complete:   function() {
            $('div#search_button').next().hide();
			$('div#search_button').show();
        },
        success:    function(data) {
        	if(data == 0){
        		$('div#services').hide();
        		return;
        	}
        	$('div#services').html(data).show();
        }
    });
	
}
// add or remove services in service group
function moveItem(option) {
	
	var selectedSeviceIds = new Array();
	var groupId = $("#ServiceGroup").val();
	var from;
	var to;
    if (option == 'add') {
        //Append selected services in Service Group
       selectedSeviceIds = $("#unmappedServiceList").selectedValues();
       from = "unmappedServiceList";   
       to = "mappedServiceList"; 
    } else if (option == 'remove') {
        //Remove selected services in Service Group
        selectedSeviceIds = $("#mappedServiceList").selectedValues();
        from = "mappedServiceList";   
        to = "unmappedServiceList"; 
        
    }
    $.ajax({
        type:   "POST",
        url:    "../ServiceGroup/AddToServiceGroupAjax?set_ajax_view",
        data:   "option=" + option + "&groupId=" + groupId + "&selectedSeviceIds=" + selectedSeviceIds,
        beforeSend: function() {
                         
        },
        complete:   function() {
 
        },
        success:    function(data) {
        	$("#" + from).copyOptions("#" + to);
        	$("#" + from).removeOption(/./, true);
        }
       });  
    
}


function getServiceRole(){
	if($('#ServiceRole').val() == '') return false;
	$.ajax({
        type:   "GET",
        url:    "../ServiceGroup/GetServiceRoleAjax?set_ajax_view",
        data:   "&groupId=" + $('#ServiceRole').val(),
        beforeSend: function() {       
        },
        complete:   function() {
        },
        success:    function(data) {
        	if(data == 0){
        		$('div#services').hide();
        		return;
        	}
        	$('div#services').html(data).show();
        }
    });
	
}

function moveRole(option) {
	
	var selectedServiceIds = new Array();
	var groupId = $("#ServiceRole").val();
	var from;
	var to;
    if (option == 'add') {
        //Append selected services in Service Group
       selectedServiceIds = $("#notAssignedServiceList").selectedValues();
       from = "notAssignedServiceList";   
        to = "assignedServiceList"; 
    } else if (option == 'remove') {
        //Remove selected services in Service Group
        selectedServiceIds = $("#assignedServiceList").selectedValues();
        from = "assignedServiceList";   
        to = ""; 
        
    }
    if(selectedServiceIds == '') return false;
    $.ajax({
        type:   "POST",
        url:    "../ServiceGroup/AddRoleToServiceAjax?set_ajax_view",
        data:   "option=" + option + "&groupId=" + groupId + "&selectedSeviceIds=" + selectedServiceIds,
        beforeSend: function() {           
        },
        complete:   function() {
        },
        success:    function(data) {
        	if(to != ""){
        		$("#" + from).copyOptions("#" + to);
        		$("#" + from).removeOption(/./, true);
        	}else{
        		$('#notAssignedServiceList').html(data);
        		$("#" + from).removeOption(/./, true);
        	}
        }
       });  
    
}


